﻿//////////////////////////////////////////////////////////////////////////
// FileName:MenuTree
// CreateBy:DragonXiong
// CreateDate:2009-5-4
// Description:
//		Class MenuTree,used for the left menustyle.
//      
// Modify Log:090602:Change Session Method --Dragon
//            091104:Set Session of CurrentSystem When Load Menu --Dragon
//            091228:Set the SessionBox.CurrentSystem only the QueryString["SysID"] is not null or empty --Dragon
//            20100129:Fix the problem only get role function --DragonXiong
//	
// Copyright:ASL-MIS
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using BLL = Auto.Data.Systems.BLL;
using Model = Auto.Data.Systems.Model;
using Auto.Web.Common;
using Auto.Data.Security;

namespace BaseSystem
{
    public partial class MenuTree : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            //    this.Page.EnableViewState = false;
            //    //userinfo.ChkUserInfo();
            //    TreeInit();
            //}
            if (!string.IsNullOrEmpty(Request.QueryString["SysID"]))
            {
                SessionBox.CurrentSystem = Request.QueryString["SysID"];
            }
            //string sysID = Request.QueryString["SysID"];
            //if (string.IsNullOrEmpty(sysID))
            //{
            //    sysID = "_System";
            //}
            //if (!IsPostBack)
            //{
                TreeInit(SessionBox.CurrentSystem);
            //}


        }


        private void TreeInit(string sysID)
        {
            this.TreeView1.Nodes.Clear();
            //string TreeUrl = Server.MapPath("Menu.xml");

            //XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(TreeUrl);
            //LoadRootNode(xmlDoc.DocumentElement);
            //// this.TreeView1.ExpandAll();
            //BLL.SysFunctions sysFuncs = new BLL.SysFunctions();
            //DataTable topFuncs = sysFuncs.GetSysFunctionsTop(SessionBox.CultrueInfo, sysID);
            //LoadRootNode(topFuncs);

            //UsersRoles userRoles = new UsersRoles();
            //LoadRootNode(userRoles.GetUserRolesFunctions(SessionBox.User.UserID, sysID, SessionBox.CultrueInfo));
            LoadRootNode(SessionBox.User.GetUserFunctions(SessionBox.User.UserID, sysID, SessionBox.CultrueInfo));
        }

        private void LoadRootNode(DataTable topFuncs/*XmlNode xmlNode*/)
        {
            if (topFuncs.Rows.Count > 0)
            {
                DataTable originData = topFuncs.Copy();


                DataView dv = topFuncs.DefaultView;
                dv.RowFilter = string.Format("{0} IS NULL or {0}='' AND IsMenu=1", Model.SysFunctions.FIELD_ParentID);

                topFuncs = dv.ToTable(true, new string[] { Model.SysFunctions.FIELD_FuncID, Model.SysFunctions.FIELD_SysID, 
                                                               Model.SysFunctions.FIELD_URL, Model.SysFunctions.FIELD_Target, 
                                                               Model.SysFunctions.FIELD_FuncName ,Model.SysFunctions.FIELD_IsEnd});
                TreeNode node;

                foreach (DataRow dr in topFuncs.Rows)
                {
                    node = new TreeNode();
                    node.Text = dr[Model.SysFunctions.FIELD_FuncName].ToString();// dr["FuncName"].ToString();// xe.GetAttribute("Text");
                    node.Value = dr[Model.SysFunctions.FIELD_FuncID].ToString();//dr["FuncID"].ToString();
                    node.Target = dr[Model.SysFunctions.FIELD_Target].ToString(); //dr["Target"].ToString();
                    // node.ImageUrl =dr["Icon"].ToString();// xe.GetAttribute("ImageUrl");
                    string url = dr[Model.SysFunctions.FIELD_URL].ToString();//dr["URL"].ToString(); //xe.GetAttribute("NavigateUrl");
                    string Expand = "True";//xe.GetAttribute("Expand");

                    if (string.IsNullOrEmpty(url))
                    {
                        node.SelectAction = TreeNodeSelectAction.Expand;
                        if (Expand == "true")
                        {
                            node.ExpandAll();
                        }
                    }
                    else
                    {
                        node.NavigateUrl = url;
                        node.SelectAction = TreeNodeSelectAction.SelectExpand;
                    }
                    this.TreeView1.Nodes.Add(node);
                    if (!Convert.ToBoolean(dr[Model.SysFunctions.FIELD_IsEnd]/*dr["IsEnd"]*/) /*xe.ChildNodes.Count > 0*/) //LOOP LOAD
                    {
                        //BLL.SysFunctions subFuncs = new BLL.SysFunctions();
                        //DataTable subSysFuncs= subFuncs.GetSysFunctionsByParentID(SessionBox.CultrueInfo,dr[Model.SysFunctions.FIELD_SysID].ToString(),dr[Model.SysFunctions.FIELD_FuncID].ToString() /*dr["SysID"].ToString(),dr["FuncID"].ToString()*/);
                        LoadFunNode(node, originData, dr[Model.SysFunctions.FIELD_FuncID].ToString());
                    }
                }
            }
            //TreeNode node;

            //foreach (XmlElement xe in xmlNode.ChildNodes)
            //{
            //    node = new TreeNode();
            //    node.Text = xe.GetAttribute("Text");
            //    node.ImageUrl = xe.GetAttribute("ImageUrl");
            //    string url = xe.GetAttribute("NavigateUrl");
            //    string Expand = xe.GetAttribute("Expand");

            //    if (string.IsNullOrEmpty(url))
            //    {
            //        node.SelectAction = TreeNodeSelectAction.Expand;
            //        if (Expand == "true")
            //        {
            //            node.ExpandAll();
            //        }
            //    }
            //    else
            //    {
            //        node.NavigateUrl = url;
            //        node.SelectAction = TreeNodeSelectAction.SelectExpand;
            //    }
            //    this.TreeView1.Nodes.Add(node);
            //    if (xe.ChildNodes.Count > 0) //LOOP LOAD
            //    {
            //        LoadFunNode(node, xe);
            //    }
            //}
        }

        private void LoadFunNode(TreeNode treeNode, DataTable dtsubFuncs, string parentID/* XmlNode xmlNode*/)
        {
            TreeNode node;

            DataTable originData = dtsubFuncs.Copy();
            DataRow[] rows = dtsubFuncs.Select(string.Format("{0} ='{1}'", Model.SysFunctions.FIELD_ParentID, parentID));

            foreach (DataRow dr in rows/*XmlElement xe in xmlNode.ChildNodes*/)
            {
                node = new TreeNode();
                node.Text =dr[Model.SysFunctions.FIELD_FuncName].ToString(); //dr["FuncName"].ToString();// xe.GetAttribute("Text");
                node.Value=dr[Model.SysFunctions.FIELD_FuncID].ToString();//dr["FuncID"].ToString();
                node.Target=dr[Model.SysFunctions.FIELD_Target].ToString();//dr["Target"].ToString();

                //node.ImageUrl = dr["Icon"].ToString();// xe.GetAttribute("ImageUrl");
                //string Expand = "True";//xe.GetAttribute("Expand");
                //if (Expand == "true")
                //{
                node.ExpandAll();
                //}
                string url =dr[Model.SysFunctions.FIELD_URL].ToString();//dr["URL"].ToString();// xe.GetAttribute("NavigateUrl");

                if (string.IsNullOrEmpty(url))
                {
                    node.SelectAction = TreeNodeSelectAction.Expand;
                }
                else
                {
                    node.SelectAction = TreeNodeSelectAction.Select;
                    TreeNode patNode = treeNode;

                    while (patNode.Parent != null)
                    {
                        patNode = patNode.Parent;
                    }

                   // node.Text = dr["FuncName"].ToString();// xe.GetAttribute("Text");
                    //node.ImageUrl = dr["Icon"].ToString();// xe.GetAttribute("ImageUrl");
                    node.NavigateUrl =url;; //xe.GetAttribute("NavigateUrl");

                }

                treeNode.ChildNodes.Add(node);

                if (!Convert.ToBoolean(dr[Model.SysFunctions.FIELD_IsEnd] /*dr["IsEnd"]*/)/*xe.ChildNodes.Count > 0*/)
                {
                    LoadFunNode(node, dtsubFuncs, dr[Model.SysFunctions.FIELD_FuncID].ToString());
                }

            }
            //TreeNode node;

            //foreach (XmlElement xe in xmlNode.ChildNodes)
            //{
            //    node = new TreeNode();
            //    node.Text = xe.GetAttribute("Text");
            //    node.ImageUrl = xe.GetAttribute("ImageUrl");
            //    string Expand = xe.GetAttribute("Expand");
            //    if (Expand == "true")
            //    {
            //        node.ExpandAll();
            //    }
            //    string url = xe.GetAttribute("NavigateUrl");

            //    if (string.IsNullOrEmpty(url))
            //    {
            //        node.SelectAction = TreeNodeSelectAction.Expand;
            //    }
            //    else
            //    {
            //        node.SelectAction = TreeNodeSelectAction.Select;
            //        TreeNode patNode = treeNode;

            //        while (patNode.Parent != null)
            //        {
            //            patNode = patNode.Parent;
            //        }

            //        node.Text = xe.GetAttribute("Text");
            //        node.NavigateUrl = xe.GetAttribute("NavigateUrl");
            //        node.ImageUrl = xe.GetAttribute("ImageUrl");
            //    }

            //    treeNode.ChildNodes.Add(node);

            //    if (xe.ChildNodes.Count > 0)
            //    {
            //        LoadFunNode(node, xe);
            //    }

            //}

        }

        #region --past--
        //private void getMenu(string mod)
        //{




        //    //MenuSystem menusystem = new MenuSystem();
        //    DataSet ds = new DataSet();
        //    ds =new BLL.SysFunctions().GetSysFunctionsForMenuTree(Session["CultureInfo"].ToString(),mod);  //menusystem.get_Menu(mod);

        //    foreach (DataRow masterRow in ds.Tables[0].Rows)
        //    {
        //        TreeNode masterNode = new TreeNode((string)masterRow["FuncName"]);
        //        TreeView1.Nodes.Add(masterNode);
        //        masterNode.SelectAction = TreeNodeSelectAction.Expand;
        //        foreach (DataRow childRow in
        //          masterRow.GetChildRows("Children"))
        //        {
        //            TreeNode childNode = new TreeNode((string)childRow["FuncName"]);
        //            // handle NULL and empty string
        //            if (childRow["URL"] is string && childRow["URL"].ToString() != "")
        //                childNode.NavigateUrl = (string)childRow["URL"];

        //            if (childRow["URL"] is string && childRow["Target"].ToString() != "")
        //                childNode.Target = (string)childRow["Target"];

        //            masterNode.ChildNodes.Add(childNode);

        //        }
        //    }
        //}
        #endregion --past--


    }
}
